<?php
/**
 * Created by PhpStorm.
 * User: xionggang
 * Date: 15-5-6
 * Time: 下午5:20
 */
class Tag_model extends MY_Model {
    public function __construct() {
        $this->_table = 'db_tag';
        parent::__construct();
    }

    /**
     * auto:xg
     * 获取标签列表
     */
    public function getlist($tagname=''){
        $tagname && $this->db->like('tagname',$tagname);
        $this->db->order_by('id', 'desc');
        $query = $this->db->get($this->_table);
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    /**
     * auto:xg
     * 保存标签
     */
    public function save_tag($id='',$date){
        if(empty($id)){
            $this->db->insert($this->_table, $date);
        }else{
            $this->db->where('id', $id);
            $this->db->update($this->_table, $date);
        }
        return $this->db->affected_rows();
    }

    /**
     * auto:
     * 获取单条标签信息
     */

    public function get_one_tag($id){
        $query = $this->db->get_where($this->_table, array('id' => $id));
        $res = $query->row_array();
        return $res;
    }
    /**
     * auto:xg
     * 根据标签名获取标签信息
     */
    public function get_tag_byname($tagname){
        if(!empty($tagname)){
            $query = $this->db->get_where($this->_table, array('tagname' => $tagname));
            $res = $query->row_array();
            if(!empty($res['tagname'])){
                return $res['tagname'];
            }else{
                return false;
            }

        }
    }
    /**
     * auto；xg
     * 删除标签
     */
    public function del_tag($id){
        $query = $this->db->delete($this->_table, array('id' => $id));
        return $this->db->affected_rows();
    }

}